<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <title>编辑商品信息</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_1128159_5o2i8ipicq.css">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <link rel="stylesheet" type="text/css" href="css/animate.css">
    <style>
        body {
            padding: 20px;
        }
        form {
            max-width: 500px;
            margin: 0 auto;
        }
        input[type="text"] {
            width: 100%;
            margin-bottom: 10px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="file"] {
            margin-bottom: 10px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: #fff;
            cursor: pointer;
        }
        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>

<?php
// 连接数据库
$host = 'localhost';
$user = 'root';
$pwd = '123456';
$dbname = 'business';

// 创建数据库连接对象
$db = new mysqli($host, $user, $pwd, $dbname);

// 检查连接是否成功
if ($db->connect_errno) {
    die("连接数据库失败: " . $db->connect_error);
}

// 设置字符集为 UTF-8
$db->set_charset('utf8');

// 获取商品ID
$id = $_GET['id'];

// 查询数据库，获取该商品的信息
$sql = "SELECT * FROM commodity WHERE Gid='$id'";
$result = $db->query($sql);

// 如果查询结果为空，则提示错误
if ($result->num_rows == 0) {
    echo "未找到该商品信息";
} else {
    // 获取商品信息
    $row = $result->fetch_assoc();
    $sname = $row['Gname'];
    $sprice = $row['Gprice'];
    $sdes = $row['Gdescription'];
    $slocation = $row['Glocation'];
    $svar = $row['Gclass'];
    $Gphone = $row['Gphone'];
    $Gbrand = $row['Gbrand'];
    $Gwechat = $row['Gwechat'];
    $Gimage = $row['Gimage'];

    // 显示表单，允许用户编辑商品信息
    echo "<form action='update_product.php' method='post' enctype='multipart/form-data'>
            <input type='hidden' name='id' value='$id'>
            <div class='form-group'>
                <label for='sname'>商品名称:</label>
                <input type='text' class='form-control' id='sname' name='sname' value='$sname'>
            </div>
            <div class='form-group'>
                <label for='sprice'>价格:</label>
                <input type='text' class='form-control' id='sprice' name='sprice' value='$sprice'>
            </div>
            <div class='form-group'>
                <label for='sdes'>描述:</label>
                <input type='text' class='form-control' id='sdes' name='sdes' value='$sdes'>
            </div>
            <div class='form-group'>
                <label for='slocation'>地点:</label>
                <input type='text' class='form-control' id='slocation' name='slocation' value='$slocation'>
            </div>
            <div class='form-group'>
                <label for='svar'>商品类别:</label>
                <input type='text' class='form-control' id='svar' name='svar' value='$svar'>
            </div>
            <div class='form-group'>
                <label for='Gphone'>手机:</label>
                <input type='text' class='form-control' id='Gphone' name='Gphone' value='$Gphone'>
            </div>
            <div class='form-group'>
                <label for='Gbrand'>QQ:</label>
                <input type='text' class='form-control' id='Gbrand' name='Gbrand' value='$Gbrand'>
            </div>
            <div class='form-group'>
                <label for='Gwechat'>微信:</label>
                <input type='text' class='form-control' id='Gwechat' name='Gwechat' value='$Gwechat'>
            </div>
            <div class='form-group'>
                <label for='Gimage'>商品图片:</label><br>
                <img src='$Gimage' alt='商品图片' style='max-width: 200px;'><br>
            </div>
            <input type='submit' class='btn btn-primary' value='提交'>
          </form>";
}

// 关闭数据库连接
$db->close();
?>

</body>
</html>
